$(function () {
    BASE.COMMON.ulrContentNav();
    BASE.COMMON.urlLeftNav();
    BASE.COMMON.recommendDarenList(1,10);
    $(document).scroll(function () {
        BASE.COMMON.scrollEvent(BASE.ANSWER.getData);
    });
});
var BASE = BASE||{};
BASE.ANSWER = (function () {
    var PAGE = 2;
    /*提问的弹窗展示*/
    function showPopQues(obj) {
        BASE.POPUP.SELECTEDCOVERS = [];
        BASE.POPUP.initShowCovers();
        $('#title').val('');
        BASE.UPLOADANSWER.ue.txt.html('');
        $('#maxGoodCount').val('');
        $('#unitPrice').val('');
        var userId = BASE.COMMON.getUserId();
        if(!userId){
            location.href = '/login.html';
            return;
        }
        var flag = $(obj).data('flag');
        if(flag){
            $('#answer_pop').children('.pop-answer').removeClass('is-full-screen');
            $('#answer_pop').show();
            return;
        }
        BASE.POPUP.coversCategory();
        $(obj).data('flag',true);
        setTimeout(function () {
            $(obj).data('flag',false);
        },500);
        BASE.POPUP.getInterest('',function () {
            $('#answer_pop').show();
            $(obj).data('flag',true);
        },true);
    }
    
    /*获取提问*/
    function getData(page) {
        if(page){
            PAGE = page;
        }
        var $box = $('#middleListContent');
        var parentId = BASE.getUrlParam('parentId');
        var categoryId = BASE.getUrlParam('categoryId') || parentId;
        var orderByField = BASE.getUrlParam('orderByField');
        var beginDateTime = BASE.getUrlParam('beginDateTime');
        $box.find('.loading').show();
        var sendData = {
            pageNum:PAGE,
            pageSize:15,
            keyword:'',
            categoryId:categoryId,
            orderByField:orderByField || 'default', // default:更新时间,answerCount回答数
            beginDateTime:_getTimeStamp(beginDateTime),	//1周内,1月内,3月内,半年内
        };
        BASE.apiGet('/device/mutualQuestion/list',sendData,function (res) {
            ++PAGE;
            $box.find('.loading').hide();
            BASE.COMMON.SCROLLFLAG = true;
            if((res.nextPage == 0 && res.pages > 1) || res.pages <= 1){
                BASE.COMMON.SCROLLFLAG = false;
                if(res.pages!=0 && res.total>=8){
                    $box.find('.nomore-data').show();
                }
            }
            var tpl = template('middleListContent_tpl',{data:res.list});
            if(PAGE == 1){
                $box.find('.content').html(tpl);
            }else{
                $box.find('.content').append(tpl);
            }
        })
    }
    /*新增问题*/
    function addQuestion(obj) {
        var sendData = _getAddQesData();
        if(!sendData){
            return;
        }
        if(sendData.maxGoodCount * sendData.unitPrice >0){
            addMoneyQues(obj,sendData);
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/mutualQuestion/add',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                return;
            }
            $('#answer_pop').hide();
            location.href = '/answer.html?orderByField=default&type=helper';
        })
    }
    /*新增红包问答*/
    function addMoneyQues(obj,sendData) {
        BASE.postHttp('/device/order/mutualQuestion/buy',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        })
    }
    /*获取新增问题的数据*/
    function _getAddQesData() {
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            covers: BASE.POPUP.getCovers().join(' '),
            categoryId: $('#interestChildData option:selected').val() ||  $('#interestData option:selected').val(),
            maxGoodCount: ($('#maxGoodCount').val() || 0),
            unitPrice: $('#unitPrice').val() || 0,
        };
        if(!sendData.title){
            BASE.clearpop('请输入标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入问题描述');
            return false;
        }
        if(!sendData.categoryId){
            BASE.clearpop('请选择类别');
            return false;
        }
        if(sendData.maxGoodCount>0 && sendData.unitPrice<=0.01){
            BASE.clearpop('红包金额最小0.01');
            return false;
        }
        return sendData;
    }
    /*获取开始的时间*/
    function _getTimeStamp(type) {
        if(!type){
            return '';
        }
        var arr = ["week", "month","quarter","halfyear"];
        var arrTime = [7,32,123,181];
        var index = arr.indexOf(type);
        var current = Date.now();
        var older = current - arrTime[index] * 24 * 60 * 60 *1000;
        return older;
    }
    if(BASE.COMMON.getUserId()) {
        /*视频分享后*/
        $('body').on('click', '.share-item', function () {
            BASE.postHttp('/device/mutualAnswer/share', { id: BASE.COMMON.getId()});
        });
        $('body').on('mouseenter', '.icon-wechat', function () {
            BASE.postHttp('/device/mutualAnswer/share', { id: BASE.COMMON.getId()});
        });
    }
    return {
        showPopQues:showPopQues,
        addQuestion:addQuestion,
        getData:getData,
    }
})();
